package com.solotiger.douban.ui.fragment.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentTransaction;
import android.util.SparseArray;
import android.view.View;

import com.solotiger.douban.R;
import com.solotiger.douban.ui.base.BaseBarFragment;
import com.solotiger.douban.ui.base.BaseFragment;
import com.solotiger.douban.ui.fragment.movie.ComingFragment;
import com.solotiger.douban.ui.fragment.movie.TheaterFragment;
import com.solotiger.douban.ui.fragment.movie.TopFragment;

import butterknife.BindView;

public class MovieFragment extends BaseBarFragment implements TabLayout.OnTabSelectedListener {


    public enum FragmentEnum {
        THEATER(R.id.tabTheater, TheaterFragment.class),
        COMING(R.id.tabComing, ComingFragment.class),
        TOP250(R.id.tabTop250, TopFragment.class);

        //外层LayoutId
        public final int tabId;
        // 对应Fragment的class
        public final Class<? extends BaseBarFragment> fragmentClass;

        private FragmentEnum(int tabId,
                             Class<? extends BaseBarFragment> fragmentClass) {
            this.tabId = tabId;
            this.fragmentClass = fragmentClass;
        }

        // 默认打开的Fragment
        public final static FragmentEnum DEFAULT = THEATER;
    }

    private final SparseArray<BaseBarFragment> fragmentArray = new SparseArray<>();

    @BindView(R.id.tabs)
    TabLayout mTabLayout;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTabLayout.addOnTabSelectedListener(this);

        setTabSelection(FragmentEnum.DEFAULT);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_moive;
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 2:
                setTabSelection(FragmentEnum.TOP250);
                break;
            case 0:
                setTabSelection(FragmentEnum.THEATER);
                break;
            case 1:
                setTabSelection(FragmentEnum.COMING);
                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    public void setTabSelection(FragmentEnum fragmentEnum) {
        if (fragmentEnum == null) {
            fragmentEnum = FragmentEnum.DEFAULT;
        }

        // 当前Fragment已经是选中状态则直接返回
        BaseBarFragment fragment = fragmentArray.get(fragmentEnum.tabId);
        if (fragment != null && fragment.isFront()) {
            return;
        }


        // 隐藏其他Fragment
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        for (int i = 0, len = fragmentArray.size(); i < len; i++) {
            BaseFragment f = fragmentArray.valueAt(i);
            transaction.hide(f);
        }
        if (fragment == null) {
            // 新建Fragment
            try {
                fragment = fragmentEnum.fragmentClass.newInstance();
                fragmentArray.put(fragmentEnum.tabId, fragment);
                transaction.add(R.id.tab_content, fragment);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            // 显示已有的Fragment
            transaction.show(fragment);
        }
        transaction.commitAllowingStateLoss();
    }
}
